package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.wanho.entity.Student;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Author：汤小洋
 * Date：2025-10-24 10:31
 * Description：<描述>
 */
@RestController
@RequestMapping("/doc")
@RequiredArgsConstructor
@Slf4j
public class DocController {

    private final ElasticsearchRestTemplate restTemplate;

    @RequestMapping("/create")
    public String create(){
        restTemplate.save(new Student(1, "alice", 20, "男", "北京大学"));

        return "success";
    }

    /**
     * 批量插入
     */
    @RequestMapping("/batch")
    public String batch(){
        List<Student> list = new ArrayList<>();
        list.add(new Student(2, "tom", 22, "男", "南京大学"));
        list.add(new Student(3, "jerry", 23, "女", "上海交通大学"));
        list.add(new Student(4, "lucy", 24, "女", "浙江大学"));
        list.add(new Student(5, "lili", 25, "女", "上海交通大学"));
        restTemplate.save(list);

        return "success";
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Student getById(@PathVariable String id){
        return restTemplate.get(id, Student.class);
    }

    /**
     * 查询所有
     */
    @GetMapping("/findAll")
    public List<Student> findAll(){
        SearchHits<Student> hits = restTemplate.search(Query.findAll(), Student.class);
        List<Student> list = hits.stream()
                .map(hit -> hit.getContent())
                .collect(Collectors.toList());
        return list;
    }

    /**
     * 根据条件查询
     */
    @GetMapping("/query")
    public List<Student> query(){

        log.info("执行了query方法。。。。。。。。。。。");

        try{
            int i = 5/0;
        }catch (Exception e){
            log.error("出现异常了。。。。。。。。。。。" + e.getMessage());
        }


        // QueryBuilder queryBuilder = QueryBuilders.matchQuery("school", "南京");

        QueryBuilder queryBuilder = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("school", "南京"))
                .must(QueryBuilders.rangeQuery("age").gte(21));

        NativeSearchQuery query = new NativeSearchQuery(queryBuilder);
        SearchHits<Student> hits = restTemplate.search(query, Student.class);

        List<Student> list = hits.stream()
                .map(hit -> hit.getContent())
                .collect(Collectors.toList());
        return list;
    }
}
